﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataSet_To_MySQL
{
    public class QueryStringBuilder
    {
        public static StringBuilder connectionStringBuilder;
        public static StringBuilder selectStringBuilder;
        # region 创建连接字符串
        /// <summary>
        /// 创建连接字符串
        /// </summary>
        /// <param name="dataBaseName"></param>
        /// <param name="dataSource"></param>
        /// <param name="userID"></param>
        /// <param name="password"></param>
        /// <param name="isPooling"></param>
        /// <param name="charSet"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public string ConnectionStringBuild(string dataBaseName, string dataSource, string userID, string password, bool isPooling, string charSet, string port)
        {
            connectionStringBuilder = new StringBuilder("Database=");
            connectionStringBuilder.Append(dataBaseName);
            connectionStringBuilder.Append("; DataSource=");
            connectionStringBuilder.Append(dataSource);
            connectionStringBuilder.Append("; UserID=");
            connectionStringBuilder.Append(userID);
            connectionStringBuilder.Append("; Password=");
            connectionStringBuilder.Append(password);
            connectionStringBuilder.Append("; pooling=");
            connectionStringBuilder.Append(isPooling);
            connectionStringBuilder.Append("; Charset=");
            connectionStringBuilder.Append(charSet);
            connectionStringBuilder.Append("; port=");
            connectionStringBuilder.Append(port);
            return connectionStringBuilder.ToString();
        }
        # endregion

        # region 创建查询字符串
        /// <summary>
        /// 创建查询字符串
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <param name="conditionColumnName"></param>
        /// <param name="conditionValue"></param>
        /// <param name="conditionValueType"></param>
        /// <returns></returns>
        public string SelectQueryStringBuilder(List<string> tableName,List<string> columnName,List<string> conditionColumnName,List<string> conditionValue,List<string> conditionValueType)
        {
            selectStringBuilder = new StringBuilder();
            selectStringBuilder.Append("SELECT ");
            if (columnName == null)
            {
                selectStringBuilder.Append("* ");
            }
            else
            {
                for (int i = 0; i < columnName.Count; i++)
                {
                    selectStringBuilder.Append(columnName[i].ToString());
                    if (i != columnName.Count - 1)
                    {
                        selectStringBuilder.Append(", ");
                    }
                    else
                    {
                        selectStringBuilder.Append(" ");
                    }
                }
            }
            selectStringBuilder.Append("FROM ");
            for (int i = 0; i < tableName.Count; i++)
            {
                selectStringBuilder.Append(tableName[i].ToString());
                if (i != tableName.Count - 1)
                {
                    selectStringBuilder.Append(", ");
                }
                else
                {
                    selectStringBuilder.Append(" ");
                }
            }
            selectStringBuilder.Append("WHERE ");
            for (int i = 0; i < conditionColumnName.Count; i++)
            {
                selectStringBuilder.Append(conditionColumnName[i].ToString());
                selectStringBuilder.Append(" = ");
                if (conditionValueType[i].ToString() == "0")
                {
                    selectStringBuilder.Append(conditionValue[i].ToString());
                }
                else
                {
                    selectStringBuilder.Append("'" + conditionValue[i].ToString() + "'");
                }
                if (i != conditionColumnName.Count - 1)
                {
                    selectStringBuilder.Append(", ");
                }
                else
                {
                    selectStringBuilder.Append(" ");
                }
            }
            //ORDER BY
            //selectStringBuilder.Append("")
            
            selectStringBuilder.Append(";");
            return selectStringBuilder.ToString();
        }
        # endregion

        # region 创建插入字符串
        //public string InsertQueryStringBuilder()
        //{

        //}
        # endregion

        # region 创建更新字符串
        //public string UpdateQueryStringBuilder()
        //{

        //}
        # endregion

        # region 创建查询字符串
        //public string DeleteQueryStringBuilder()
        //{

        //}
        # endregion
    }
}
